﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
  public  class CBizShpCustomerInfo: CustBizObject
    {
        private CDmShpCustomerInfo m_CDmShpCustomerInfo = new CDmShpCustomerInfo();
        private CDmShpCustomerInfo oDmShpCustomerInfo
        {
            get { return m_CDmShpCustomerInfo; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizShpCustomerInfo()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmShpCustomerInfo.SHP_CUSTOMER_INFO);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["USER_NME"]; 
            //this.DataObj.IdGroup = "RID";
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            drw["SCORE_QTY"] = 0;
            drw["CHARGE_AMT"] = 0;
            drw["U_MONEY_QTY"] = 0;
            drw["PROMOTION_SCORE_QTY"] = 0;
            drw["PROMOTION_CHARGE_AMT"] = 0;
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
       
        public DataSet SelectByUserNmeS(string strUserNmeS)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  USER_NME IN  ('" + strUserNmeS.Replace(",","','") + "')");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectByUserNme(string strUserNme)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  USER_NME =  '" + strUserNme + "'");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectUserLedgerStatistic(string strusernme)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
    SELECT    
     WAIT_PAY_COUNT =SUM(CASE WHEN A.TRADING_SID IS NOT NULL AND A.TRADING_SID='待支付' THEN 1 ELSE 0 END)
    ,COMP_PAY_COUNT=SUM(CASE WHEN A.TRADING_SID IS NOT NULL AND A.TRADING_SID='已支付' THEN 1 ELSE 0 END) 
    ,CAC_PAY_COUNT=SUM(CASE WHEN A.TRADING_SID IS NOT NULL AND A.TRADING_SID='已取消' THEN 1 ELSE 0 END) 
    ,PAY_COUNT =COUNT(*)
    FROM SHP_CHARGE_TRADING_LOG A
    WHERE
     ( A.ACCOUNT_NO='" + strusernme + @"' )
");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public decimal GetUserChargeAmt(string strUserNme)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
    SELECT
    CHARGE_AMT= ISNULL(B.CHARGE_AMT,0) 
    FROM SHP_CUSTOMER_INFO B WHERE B.USER_NME='" + strUserNme + "' ");
            if (ds.Tables[0].Rows.Count > 0 )
            {
                return (decimal)ds.Tables[0].Rows[0]["CHARGE_AMT"];
            }
            return 0M;
        }

        /// <summary>
        /// 获取可用积分
        /// </summary>
        /// <param name="strUserNme"></param>
        /// <returns></returns>
        public int GetUserScoreAllowUseQty(string strUserNme)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
    SELECT
    SCORE_QTY= ISNULL(B.SCORE_QTY,0) 
    FROM SHP_CUSTOMER_INFO B WHERE B.USER_NME='"+strUserNme+"' ");
            if (ds.Tables[0].Rows.Count > 0)
            {
                return (int)ds.Tables[0].Rows[0]["SCORE_QTY"];
            }
            return 0;
        }

        /// <summary>
        /// 获取可用优币
        /// </summary>
        /// <param name="strUserNme"></param>
        /// <returns></returns>
        public int GetUserUMoneyAllowUseQty(string strUserNme)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
    SELECT
    U_MONEY_QTY= ISNULL(B.U_MONEY_QTY,0) 
    FROM SHP_CUSTOMER_INFO B WHERE B.USER_NME='" + strUserNme + "' ");
            if (ds.Tables[0].Rows.Count > 0)
            {
                return (int)ds.Tables[0].Rows[0]["U_MONEY_QTY"];
            }
            return 0;
        }

      /// <summary>
        /// 获取推广可用金额
      /// </summary>
      /// <param name="strUserNme"></param>
      /// <returns></returns>
        public decimal GetUserPromotionChargeAmt(string strUserNme)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
    SELECT
    CHARGE_AMT= ISNULL(B.PROMOTION_CHARGE_AMT,0) 
    FROM SHP_CUSTOMER_INFO B WHERE B.USER_NME='" + strUserNme + "' ");
            if (ds.Tables[0].Rows.Count > 0)
            {
                return (decimal)ds.Tables[0].Rows[0]["CHARGE_AMT"];
            }
            return 0M;
        }

        /// <summary>
        /// 获取推广可用积分
        /// </summary>
        /// <param name="strUserNme"></param>
        /// <returns></returns>
        public int GetUserPromotionScoreQty(string strUserNme)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
    SELECT
    SCORE_QTY= ISNULL(B.PROMOTION_SCORE_QTY,0) 
    FROM SHP_CUSTOMER_INFO B WHERE B.USER_NME='" + strUserNme + "' ");
            if (ds.Tables[0].Rows.Count > 0)
            {
                return (int)ds.Tables[0].Rows[0]["SCORE_QTY"];
            }
            return 0;
        }

        public DataSet SelectUserStatistic(string strusernme)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
    SELECT    
     WAIT_PAY_COUNT =SUM(CASE WHEN A.TRADE_STATUS_TXT IS NOT NULL AND A.TRADE_STATUS_TXT='等待买家付款' THEN 1 ELSE 0 END)
    ,WAIT_SHIPPING_COUNT=SUM(CASE WHEN A.TRADE_STATUS_TXT IS NOT NULL AND A.TRADE_STATUS_TXT='买家已付款' THEN 1 ELSE 0 END)
    ,WAIT_RECEIPT_COUNT=SUM(CASE WHEN A.TRADE_STATUS_TXT IS NOT NULL AND A.TRADE_STATUS_TXT='卖家已发货' THEN 1 ELSE 0 END)
    ,WAIT_RETURN_COUNT=SUM(CASE WHEN A.TRADE_STATUS_TXT IS NOT NULL AND A.TRADE_STATUS_TXT='退款中的订单' THEN 1 ELSE 0 END)
    ,WAIT_REVIEW_COUNT =SUM(CASE WHEN A.BUYER_RATE_SID='0' THEN 1 ELSE 0 END)
    FROM SHP_ORDER_FORM A
    WHERE
    (A.TRADE_STATUS_TXT='等待买家付款' OR A.TRADE_STATUS_TXT='退款中的订单' OR A.TRADE_STATUS_TXT='买家已付款' OR A.TRADE_STATUS_TXT='卖家已发货' OR A.BUYER_RATE_SID='0') AND  ( A.USER_NME='" + strusernme + @"' )
");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }


        public DataSet SelectInfoForFinder(string strUserNme, string strSid)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("strUserNme", ParameterDirection.Input, EDbType.String, 36, strUserNme));
            parmCollection.Add(new CDbParm("strSid", ParameterDirection.Input, EDbType.String, 2, strSid));
            DataSet ds = this.DataObj.QueryBySP("SHP_SELECT_CUSTOMER_INFO_FINDER", parmCollection);
            ds.Tables[0].TableName = this.TableName;
            return ds; 
        }




    }
}
